// const app = require('express')()

//导入uuid模块
const uuid = require('node-uuid')

//导入数据库操作模块
const db = require("../../db/index")


/**
 * 获取学习视频列表
 * @param {*} req 
 * @param {*} res 
 */
exports.getStudyVideosList = async (req, res) => {
    // console.log('获取学习视频列表'+req.ip)
    const tag = req.query.tag
    let sql = ''
    if (!tag || tag == "" || tag + "" == "全部") {
        sql = `SELECT videos.id,videos.title,tags.tag_name AS tags,videos.description,videos.video_url,videos.img_url,videos.flag FROM xks_study_videos AS videos, xks_tags AS tags WHERE tags.tag_type = "学习视频" AND videos.id=tags.c_id `;
        await db.query(sql, (err, results) => {
            if (err) {
                return res.send({
                    code: 505,
                    msg: '发送了错误' + err.message,
                    data: {}
                })
            }

            if (!results.length > 0) {
                return res.send({
                    code: 400,
                    msg: '抱歉，没有找到你想要的内容',
                    data: {}
                })
            }

            return res.send({
                code: 200,
                msg: '成功',
                data: {
                    list: results
                }
            })
        })
    }
    else {
        sql = `SELECT videos.id,videos.title,tags.tag_name AS tags,videos.description,videos.video_url,videos.img_url,videos.flag FROM xks_study_videos AS videos, xks_tags AS tags WHERE tags.tag_type = "学习视频" AND videos.id=tags.c_id AND tags.tag_name = ?`;
        await db.query(sql, tag, (err, results) => {
            if (err) {
                return res.send({
                    code: 505,
                    msg: '获取失败，发送了错误' + err.message,
                    data: {}
                })
            }

            if (!results.length > 0) {
                return res.send({
                    code: 400,
                    msg: '抱歉，没有找到你想要的内容',
                    data: {}
                })
            }

            return res.send({
                code: 200,
                msg: '成功',
                data: {
                    list: results
                }
            })
        })
    }
}

/**
 * 根据标题获取视频列表（模糊匹配）
 * @param {*} req 
 * @param {*} res 
 */
exports.getStudyVideosListByTitle = (req, res) => {
    // console.log('获取学习视频列表'+req.ip)
    const title = req.query.title
    // console.log('title' + title)
    let sql = ''
    if (!title || title == "") {
        sql = `SELECT videos.id,videos.title,tags.tag_name AS tags,videos.description,videos.video_url,videos.img_url,videos.flag FROM xks_study_videos AS videos, xks_tags AS tags WHERE tags.tag_type = "学习视频" AND videos.id=tags.c_id `;
        db.query(sql, (err, results) => {
            if (err) {
                return res.send({
                    code: 505,
                    msg: '发送了错误' + err.message,
                    data: {}
                })
            }

            if (!results.length > 0) {
                return res.send({
                    code: 400,
                    msg: '抱歉，没有找到你想要的内容',
                    data: {}
                })
            }

            return res.send({
                code: 200,
                msg: '成功',
                data: {
                    list: results
                }
            })
        })
    }
    else {
        sql = `SELECT videos.id,videos.title,tags.tag_name AS tags,videos.description,videos.video_url,videos.img_url,videos.flag FROM xks_study_videos AS videos, xks_tags AS tags WHERE videos.title LIKE concat('%',?, '%') AND tags.tag_type = "学习视频" AND videos.id=tags.c_id `;
        db.query(sql, title, (err, results) => {
            if (err) {
                return res.send({
                    code: 505,
                    msg: '获取失败，发送了错误' + err.message,
                    data: {}
                })
            }

            if (!results.length > 0) {
                return res.send({
                    code: 400,
                    msg: '抱歉，没有找到你想要的内容',
                    data: {}
                })
            }

            return res.send({
                code: 200,
                msg: '成功',
                data: {
                    list: results
                }
            })
        })
    }
}
